//
//  MyExpressTableViewController.m
//  Express
//
//  Created by lanou on 16/1/8.
//  Copyright © 2016年 FJY. All rights reserved.
//

#import "MyExpressTableViewController.h"
#import "BaseHeader.h"


@interface MyExpressTableViewController ()

@property (nonatomic, strong) NSMutableArray *modelsArray;

@end

@implementation MyExpressTableViewController

- (NSMutableArray *)modelsArray {
    if (!_modelsArray) {
        _modelsArray = [[NSMutableArray alloc] init];
    }
    return _modelsArray;
}

- (void)viewWillAppear:(BOOL)animated {
    [self.modelsArray removeAllObjects];
    self.modelsArray = [[HistoryDataBase shareHistoryDataBase] selectMultipleDataFromCloudDB];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyExpressLoadData) name:@"myExpressQuery" object:nil];
    
    
    [self.tableView registerClass:[MyExpressTableViewCell class] forCellReuseIdentifier:@"myExpressCell"];
    
}

- (void)MyExpressLoadData {
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyExpressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myExpressCell" forIndexPath:indexPath];
    cell.express = self.modelsArray[self.modelsArray.count - 1 - indexPath.row];
    cell.number.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 1];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CATransition *transition = [CATransition animation];
    transition.type = @"rippleEffect";
    transition.subtype = kCATransitionFade;
    transition.duration = 2;
    [self.view.layer addAnimation:transition forKey:@"waterDrop"];
    QueryResultTableViewController *queryResult = [[QueryResultTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
    queryResult.express = self.modelsArray[self.modelsArray.count - 1 - indexPath.row];
    [self.navigationController pushViewController:queryResult animated:YES];

}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [[HistoryDataBase shareHistoryDataBase] deleteSingleDataFromCloudDBWithModel:self.modelsArray[self.modelsArray.count - 1 - indexPath.row]];
        [self.modelsArray removeObjectAtIndex:self.modelsArray.count - 1 - indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
